package br.com.os2.pacemark.gui;

import br.com.os2.pacemark.dao.ProdutoDAO;
import br.com.os2.pacemark.model.Produto;
import br.com.os2.pacemark.util.DateUtil;
import br.com.os2.pacemark.util.Report;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author Daniel
 * 
 */
public class Relatorios extends javax.swing.JDialog {

    public Relatorios(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbRelatorio = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Relatório");

        cmbRelatorio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Produtos" }));

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(cmbRelatorio, 0, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbRelatorio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        List<Produto> produtos = new ProdutoDAO().listar();

            if (produtos == null || produtos.size() <= 0) {
                JOptionPane.showMessageDialog(null, 
                                             "Não há despesas no mês escolhido.", 
                                             "Documento não tem páginas.", 
                                              JOptionPane.INFORMATION_MESSAGE);
            } else {
                
                List dados = new ArrayList();

                Map registro = null;
                
                for(Produto p : produtos){
                    
                    registro = new HashMap();
                    
                    registro.put("lote", p.getLote());
                    
                    registro.put("nome", p.getNome());
                    
                    registro.put("quantidade", p.getQuantidade());
                    
                    registro.put("vencimento", DateUtil.format(p.getVencimento()));
                    
                    dados.add(registro);
                }

                JRBeanCollectionDataSource fonteDeDados = new JRBeanCollectionDataSource(dados);
                
                try {
                    
                    Report.gerarRelatorioDesktop(cmbRelatorio.getSelectedItem()+".jasper", null, fonteDeDados);
                    
                } catch (JRException ex) {
                    JOptionPane.showMessageDialog(null, "Erro ao gerar Relatório " + ex);
                }
 
            }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbRelatorio;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
